
Imports std
Imports System.IO
Imports language.japanese

Public Module jpime

    Friend Function isConvertableBackChar(ByVal c As Char) As Boolean
        Return isspace(c) OrElse language.japanese.possibleChar(c)
    End Function

    Friend Function isConvertableBack(ByRef s As String) As Boolean
        Return strlen(s) <= 64 AndAlso strform(s, AddressOf isConvertableBackChar)
    End Function

    Friend Function isConvertableChar(ByVal c As Char) As Boolean
        Return c = character.minusSign OrElse isspace(c) OrElse language.english.isalpha(c)
    End Function

    Friend Function isConvertableKana(ByVal c As Char) As Boolean
        Return isspace(c) OrElse iskana(c)
    End Function

    Friend Function isConvertable(ByRef s As String, ByVal kanamode As Boolean) As Boolean
        If kanamode Then
            Return strform(s, AddressOf isConvertableKana)
        Else
            Return strform(s, AddressOf isConvertableChar)
        End If
    End Function

    Public Sub main(ByVal args() As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim rst As String = Nothing
        Dim safeMode As Boolean = False
        Dim easyIME As language.japanese.easyIME = Nothing
        Dim kanaMode As Boolean = False
        Dim i As Int32

        If isdebugmode() Then
            pause()
        End If

        If args.Length() < 2 Then
            raiseError("argument count, input output safe? [easyIME? dict], if input or output is '/-', use console", errorHandle.errorType.critical)
        Else
            If args(0) = "/-" Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(0), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            assert(Not reader Is Nothing, "file no found, " + args(0))

            If args(1) = "/-" Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(1), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not writer Is Nothing, "file no found, " + args(1))

            For i = 2 To args.Length() - 1
                If strsame(args(i), "safe", False) Then
                    safeMode = True
                ElseIf strsame(args(i), "easyime", False) AndAlso i < args.Length() - 1 Then
                    easyIME = New language.japanese.easyIME
                    easyIME.initial(args(i + 1))
                ElseIf strsame(args(i), "kanamode", False) Then
                    kanaMode = True
                End If
            Next

            input = reader.ReadLine
            While Not input Is Nothing
                writer.Write(input)
                writer.Write(character.tab)
                kickLeftRightBlank(input)
                If isConvertableBack(input) Then
                    If kanaMode Then
                        If easyIME Is Nothing Then
                            rst = any2hira(input)
                        Else
                            rst = easyIME.any2kana(input)
                        End If
                    Else
                        If easyIME Is Nothing Then
                            rst = any2pron(input, 1)
                        Else
                            rst = easyIME.any2pron(input, 1)
                        End If
                    End If
                    If safeMode Then
                        If isConvertable(rst, kanaMode) Then
                            writer.Write(rst)
                        End If
                    Else
                        writer.Write(rst)
                    End If
                End If
                writer.Write(character.tab)
                If isConvertable(input, False) Then
                    rst = pron2norm(input)
                    If safeMode Then
                        If isConvertableBack(rst) Then
                            writer.Write(rst)
                        End If
                    Else
                        writer.Write(rst)
                    End If
                End If
                writer.WriteLine()
                input = reader.ReadLine
            End While
            closestream(writer)
        End If

    End Sub

End Module
